Obsidian 次のバックリンクを選択
NextBacklink
カーソル位置がバックリンクの末尾に移動するように変更
完了時のポップアップをオフに
code:js
<%*
const editor = this.app.workspace.activeEditor.editor; // アクティブなエディタを取得
const cursor = editor.getCursor(); // 現在のカーソル位置を取得
const content = editor.getValue(); // 全テキストを取得
const backlinkPattern = /\[\[(^\]+)\]\]/g; // バックリンクの正規表現パターン let nextBacklinkIndex = -1;
let currentBacklinkEnd = -1;
// 現在のカーソル位置のオフセットを取得
const cursorOffset = editor.posToOffset(cursor);
// すでに選択されているバックリンクをチェック
backlinkPattern.lastIndex = 0; // 正規表現のインデックスを初期化
let match;
while ((match = backlinkPattern.exec(content)) !== null) {
const startIndex = match.index;
const endIndex = backlinkPattern.lastIndex;
// カーソルが現在のバックリンク内にあるか確認
if (cursorOffset >= startIndex && cursorOffset <= endIndex) {
currentBacklinkEnd = endIndex;
break;
}
}
// 次のバックリンクを現在のバックリンクの後から検索
if (currentBacklinkEnd !== -1) {
backlinkPattern.lastIndex = currentBacklinkEnd; // 現在のバックリンクの終わりから検索開始
} else {
backlinkPattern.lastIndex = cursorOffset; // 現在のカーソル位置から検索開始
}
while ((match = backlinkPattern.exec(content)) !== null) {
nextBacklinkIndex = backlinkPattern.lastIndex;
break;
}
// 次のバックリンクが見つかった場合、その末尾にカーソルを移動
if (nextBacklinkIndex !== -1) {
const nextBacklinkPos = editor.offsetToPos(nextBacklinkIndex); // バックリンクの末尾にカーソルを移動
editor.setCursor(nextBacklinkPos);
// new Notice("次のバックリンクに移動しました。");
} else {
new Notice("次のバックリンクが見つかりませんでした。");
}
%>
code:js
<%*
const editor = this.app.workspace.activeEditor.editor; // アクティブなエディタを取得
const cursor = editor.getCursor(); // 現在のカーソル位置を取得
const content = editor.getValue(); // 全テキストを取得
const backlinkPattern = /\[\[(^\]+)\]\]/g; // バックリンクの正規表現パターン let nextBacklinkIndex = -1;
let currentBacklinkEnd = -1;
// 現在のカーソル位置のオフセットを取得
const cursorOffset = editor.posToOffset(cursor);
// すでに選択されているバックリンクをチェック
backlinkPattern.lastIndex = 0; // 正規表現のインデックスを初期化
let match;
while ((match = backlinkPattern.exec(content)) !== null) {
const startIndex = match.index;
const endIndex = backlinkPattern.lastIndex;
// カーソルが現在のバックリンク内にあるか確認
if (cursorOffset >= startIndex && cursorOffset <= endIndex) {
currentBacklinkEnd = endIndex;
break;
}
}
// 次のバックリンクを現在のバックリンクの後から検索
if (currentBacklinkEnd !== -1) {
backlinkPattern.lastIndex = currentBacklinkEnd; // 現在のバックリンクの終わりから検索開始
} else {
backlinkPattern.lastIndex = cursorOffset; // 現在のカーソル位置から検索開始
}
while ((match = backlinkPattern.exec(content)) !== null) {
nextBacklinkIndex = backlinkPattern.lastIndex;
break;
}
// 次のバックリンクが見つかった場合、その位置にカーソルを移動
if (nextBacklinkIndex !== -1) {
const nextBacklinkPos = editor.offsetToPos(nextBacklinkIndex - match0.length); editor.setCursor(nextBacklinkPos);
new Notice("次のバックリンクに移動しました。");
} else {
new Notice("次のバックリンクが見つかりませんでした。");
}
%>